import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import AntToolTip from "~/public/cpas-ui/components-ui/ant-components/AntToolTip";
import SelectBox from "devextreme-react/select-box";
import CpasIcon from "~/public/cpas-ui/components-ui/components/Icon";
import { AntInput } from "~/public/cpas-ui/index";
import { message } from "antd";
// 列编辑, 目前列移动仅支持单个列移动 2023年04月18日15:22:09 厂哥丽莎 上午需求确定.
const ColumnEditor = ({ _this, columnTagPolymericRef, columnTagToolItemRef, }) => {
    //声明整理模式下移动列的函数
    const handleMoveColumn = async (direction) => {
        // _this.gridRef.manualColumnMovePlugin.moveColumns([2, 3, 4], 5);
        // _this.gridRef.hot.render();
        //获取要移动的列
        const { computedMovesColumns, matchSelectTopHeader } = _this.headerService.getMoveColumns(direction);
        if (computedMovesColumns.length > 0) {
            //调用移动列服务
            await beginMoveColumn(computedMovesColumns, direction);
        }
        _this.gridRef.setTableContentChange(true, {
            type: "列移动",
            params: { computedMovesColumns, direction },
        });
    };
    // computerNeedReverseGroupInfo
    const handleReverse = async () => {
        //一. 先逆序一组
        // const reverseInfo = sessionStorage.reverseInfo ? JSON.parse(sessionStorage.reverseInfo) : undefined;
        // let needReverseGroupInfo = [];
        // if(!reverseInfo) { // 不是导入的json的逆序
        //   needReverseGroupInfo = computerNeedReverseGroupInfo();
        // }else { // 导入的json 后的逆序
        //   needReverseGroupInfo = reverseInfo.needReverseGroupInfo;
        // }
        //let needReverseGroupInfo = [];
        // if(!reverseInfo) { // 不是导入的json的逆序
        //   needReverseGroupInfo = _this.needReverseGroupInfo;
        // }else { // 导入的json 后的逆序
        //   needReverseGroupInfo = reverseInfo.needReverseGroupInfo;
        // }
        console.log("---------------look here----------------");
        console.log("needReverseGroupInfo:", _this.needReverseGroupInfo);
        await reverse(_this.needReverseGroupInfo);
    };
    const reverse = async (needReverseGroupInfo) => {
        const isReverse = !_this.state.isReverse; // false
        _this.setState({
            isReverse,
        });
        const reverseInfo = {
            needReverseGroupInfo,
            isReverse,
        };
        sessionStorage.reverseInfo = JSON.stringify(reverseInfo);
        _this.gridRef.tableConfig.reverseInfo = reverseInfo;
        for (let i = 0; i < needReverseGroupInfo.length; i++) {
            const singleReverseGroupInfo = needReverseGroupInfo[i];
            await reverseOneGroup(singleReverseGroupInfo);
        }
    };
    const reverseOneGroup = async (singleReverseGroupInfo) => {
        const reverseStart = singleReverseGroupInfo.reverseStart;
        const reverseEnd = singleReverseGroupInfo.reverseEnd; // 6
        const elementCount = singleReverseGroupInfo.elementCount; // 5
        const computedMovesColumnsArr = [];
        //i=1,2
        //j=0,1
        for (let i = 1; i <= elementCount - 1; i++) {
            // 控制移动几个元素
            // 移动一个元素
            for (let j = 0; j < i; j++) {
                // 每个元素移动几次
                const computedMovesColumns = [
                    { index: reverseEnd - i + j, target: reverseEnd - i + j + 1 },
                ];
                computedMovesColumnsArr.push(computedMovesColumns);
            }
        }
        for (let i = 0; i < computedMovesColumnsArr.length; i++) {
            await beginMoveColumn(computedMovesColumnsArr[i], "right");
        }
    };
    const beginMoveColumn = async (computedMovesColumns, direction) => {
        const { gridData, computedAfterMoveRange } = _this.headerService.moveColumnsByComputedMovesColumns(computedMovesColumns, direction);
        //刷新表格
        // _this.hot.render();
        // TODO 利用重载的方式可以避免列样式重叠的问题. 但是会重刷当前表格状态。无法保留之前的交互状态. 例如：选中的单元格。 而且会妨碍会影响回撤功能。但是回撤功能目前还没有实现。实现机制可以根据刷表的方式回撤
        await _this.gridRef.reloadTable(gridData);
        //列移动后状态回显
        _this.gridRef.hot.selectColumns(computedAfterMoveRange[0], computedAfterMoveRange[computedAfterMoveRange.length - 1]);
        // TODO 支持多列选中的表头，自实现方式通过递归dom方式. 但是会有选中状态问题。由于现在改成了单列移动，所以暂时不考虑多列移动的情况
        // const topLevelHeaderKeys = matchSelectTopHeader.map((item) => {
        //   return item.key;
        // });
        // _this.headerService.selectNodeByKey(topLevelHeaderKeys);
    };
    return (_jsxs("div", { className: "col col2", children: [_jsx("div", { className: "row row1", children: _jsx("span", { children: "\u5217\u7F16\u8F91" }) }), _jsxs("div", { className: "tools-item-container", children: [_jsxs("div", { className: "row row2", children: [_jsx("div", { className: "tools-item-inline", children: _jsx("span", { className: "input-style-min", children: "\u5217\u540D\u79F0" }) }), _jsx("div", { className: "tools-item-inline", children: _jsx(AntInput, { className: "column-name-input", size: "middle", value: _this.state.currentColumnName, onChange: (e) => {
                                        const val = e.target.value;
                                        _this.throttleColumnNameChange(e);
                                        _this.setState({
                                            currentColumnName: val,
                                        });
                                    } }) }), _jsxs("div", { className: "tools-item-inline", children: [_jsx("span", { className: "data-type", children: "\u6570\u636E\u7C7B\u578B" }), _jsx(SelectBox, { placeholder: "", displayExpr: "name", valueExpr: "id", value: _this.state.currentColumnType, onItemClick: _this.columnTypeChange, items: _this.columnTypeOption, className: "select-box-style-min" })] }), _jsx(AntToolTip, { placement: "bottom", title: "\u662F\u5426\u5C55\u793A\u767E\u5206\u6BD4\u7B26\u53F7", children: _jsx("div", { className: _this.state.percentFormat
                                        ? "tools-item cpas-menu-item-active"
                                        : "tools-item", onClick: () => {
                                        _this.setPercentFormat();
                                    }, children: _jsx("i", { className: "dx-icon-percent cpas-icon " }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u5408\u5E76\u9879\u76EE\u5217", children: _jsx("div", { className: _this.state.keywordState
                                        ? "tools-item cpas-menu-item-active"
                                        : "tools-item", onClick: () => {
                                        _this.setColumnKeyword();
                                    }, children: _jsx(CpasIcon, { type: "icon-hebingxiangmulie", className: "cpas-icon" }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u5217\u6807\u7B7E", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        _this.rowEditorService.openSetTag("column");
                                    }, id: "labelColumnPanel", ref: columnTagToolItemRef, children: _jsx(CpasIcon, { type: "icon-liebiaoqian", className: "cpas-icon" }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u5217\u6807\u7B7E\u805A\u5408", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        _this.rowEditorService.openSetTag("column", true);
                                        _this.openPolyMeric("column");
                                    }, id: "labelJuHeColumnPanel", ref: columnTagPolymericRef, children: _jsx(CpasIcon, { type: "icon-liebiaoqianjuhe", className: "cpas-icon" }) }) })] }), _jsxs("div", { className: "row row3", children: [_jsx("div", { className: "tools-item-inline", children: _jsx("span", { className: "input-style-min", children: "\u5217\u805A\u5408" }) }), _jsx("div", { className: "tools-item-inline", children: _jsx(SelectBox, { placeholder: "请选择", displayExpr: "name", valueExpr: "id", 
                                    //disabled={true}
                                    value: _this.state.cellPType, onItemClick: async (e) => {
                                        const pType = e.itemData.id;
                                        const coords = _this.gridRef.state.currentRecord.coords;
                                        //const rowIndex = coords && coords.row ? coords.row : 1;
                                        const colIndex = coords && coords.col ? coords.col : 1;
                                        if (pType === "CLEAR") {
                                            await _this.selectRowColumnTagsChange("column");
                                            await _this.juheColumnChange(e);
                                        }
                                        else {
                                            const tagName = `LEFT--${parseInt(String(10000 * Math.random())) + ""}`;
                                            if (+colIndex < 2) {
                                                return message.error("操作列聚合运算的单元格位置不对");
                                            }
                                            await _this.juheColumnChange(e);
                                            await _this.setTag("column", tagName, "left");
                                            _this.setColumnCountType(pType, [tagName], "left");
                                        }
                                        return _this.gridRef.setTableContentChange(true, {
                                            type: "列聚合",
                                            params: {
                                                value: _this.state.cellPType,
                                            },
                                        });
                                    }, 
                                    //disabled={true}
                                    items: _this.columnAggregationOption, className: "select-box-style-min" }) }), _this.gridRef.props.type === "ds" ? (_jsxs("div", { className: "tools-item-inline", children: [_jsx("span", { className: "qijian-type", children: "\u671F\u95F4\u7C7B\u578B" }), _jsx(SelectBox, { placeholder: "", displayExpr: "name", disabled: _this.state.formulaOff === true, valueExpr: "id", value: _this.state.currentQiJianType, onItemClick: (e) => _this.columnEditorService.qiJianTypeChange(e), items: _this.qiJianTypeOption, className: "select-box-style-min" })] })) : null, _jsx(AntToolTip, { placement: "bottom", title: "\u589E\u52A0\u5C0F\u6570\u4F4D", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        _this.setDp("add");
                                    }, children: _jsx(CpasIcon, { type: "icon-zengjiaxiaoshuwei", className: "cpas-icon" }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u51CF\u5C11\u5C0F\u6570\u4F4D", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        _this.setDp("minus");
                                    }, children: _jsx(CpasIcon, { type: "icon-jianshaoxiaoshuwei", className: "cpas-icon" }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u5217\u5DE6\u79FB", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        handleMoveColumn("left");
                                    }, children: _jsx(CpasIcon, { type: "icon-arrow-left", className: "cpas-icon" }) }) }), _jsx(AntToolTip, { placement: "bottom", title: "\u5217\u53F3\u79FB", children: _jsx("div", { className: "tools-item", onClick: () => {
                                        handleMoveColumn("right");
                                    }, children: _jsx(CpasIcon, { type: "icon-arrow-right", className: "cpas-icon" }) }) })] })] })] }));
};
export default ColumnEditor;
